package com.etc.cloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author kalista
 * @Description
 * @Date 2020/8/23  10:43
 **/
@Service
public class PaymentService {



    public String paymentinfo_ok(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "paymentinfo ok" + id + "0(n_n)0";
    }




    @HystrixCommand(fallbackMethod = "paymentinfo_timeoutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")})
    public String paymentinfo_timeout(Integer id){
        int tiemNumber = 5000 ;
        try {
            TimeUnit.MILLISECONDS.sleep(tiemNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
         return "线程池:" + Thread.currentThread().getName() + "paymentinfo_timeout" + id + "0(n_n)0" + "耗时:" +tiemNumber + "秒";
    }



    public String paymentinfo_timeoutHandler(Integer id){
        return "线程池:" + Thread.currentThread().getName() + "8001系统繁忙稍后在试试";
    }



    //=====服务熔断
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),// 是否开启断路器
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),// 失败率达到多少后跳闸
    })
    public String  paymentCircuitBreaker(Integer id){
        if (id < 0){
            throw  new RuntimeException("id 不能为负数");
        }
        String uuid = UUID.randomUUID().toString();
        return Thread.currentThread().getName() + "\t" + "调用成功" + uuid;
    }


    public String paymentCircuitBreaker_fallback(Integer id){
        return "id 不能负数，请稍后再试，/(ㄒoㄒ)/~~   id: " +id;
    }

}
